package com.sunlands.qdcy.tongzhuo.service;

import com.alibaba.fastjson.JSON;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.tiia.v20190529.TiiaClient;
import com.tencentcloudapi.tiia.v20190529.models.ImageModerationRequest;
import com.tencentcloudapi.tiia.v20190529.models.ImageModerationResponse;
import com.tencentcloudapi.tiia.v20190529.models.PornResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

/**
 * @author zl
 * @version 1.0
 * @date Created in 2019/11/27
 */
@Service
@Slf4j
public class TencentImageCheckService {

    @Value("${tencent.secretId}")
    private String secretId;

    @Value("${tencent.secretKey}")
    private String secretKey;

    @Value("${tencent.token}")
    private String token;

    @Value("${tencent.region}")
    private String region;

    @Bean
    private TiiaClient tiiaClient() {
        return new TiiaClient(new Credential(secretId, secretKey), region);
    }

    @Autowired
    private TiiaClient tiiaClient;


    public Boolean checkImg(String imageUrl) {

        ImageModerationRequest request = new ImageModerationRequest();
        request.setScenes(new String[]{"PORN"});
        request.setImageUrl(imageUrl);
        ImageModerationResponse response = null;
        log.debug("tencent实时鉴黄参数：{}", JSON.toJSONString(request));
        try {
            response = tiiaClient.ImageModeration(request);
        } catch (TencentCloudSDKException e) {
            log.error("腾讯云图像审核失败，request={},response={},e={}", JSON.toJSONString(request), JSON.toJSONString(response), e);
        }

        if (response == null || response.getPornResult() == null) {
            log.error("腾讯云图像审核未响应鉴别结果，request={}", JSON.toJSONString(request));
            return false;
        }

        PornResult pornResult = response.getPornResult();
        if (0 != pornResult.getCode()) {
            log.error("腾讯云图像审核检测结果失败，request={}，pornResult={}", JSON.toJSONString(request), JSON.toJSONString(pornResult));
            return false;
        }
        log.debug("tencent实时鉴黄响应：code={},pornResult={}", pornResult.getCode(), JSON.toJSONString(pornResult));
        return imageIsPorn(pornResult);
    }


    public boolean imageIsNomal(PornResult pornResult) {
        if ("PASS".equals(pornResult.getSuggestion()) || pornResult.getConfidence() < 90) {
            return true;
        }
        return false;
    }

    public boolean imageIsPorn(PornResult pornResult) {
        if (!"PASS".equals(pornResult.getSuggestion()) && pornResult.getConfidence() > 98) {
            return true;
        }
        return false;
    }


    public String getCodeMsg(Long code) {
        if (code == null) {
            return null;
        }

        switch (code.intValue()) {
            case 0:
                return "成功";
            case -1:
                return "系统错误";
            case -2:
                return "引擎错误";
            case -1400:
                return "图片解码失败";
            default:
                return code.toString();
        }

    }

    public String getSuggestionMsg(String suggestion) {
        if (StringUtils.isBlank(suggestion)) {
            return "";
        }
        switch (suggestion) {
            case "PASS":
                return "正常";
            case "REVIEW":
                return "疑似";
            case "BLOCK":
                return "违规";
            default:
                return suggestion;

        }

    }


    public String getErrorMsg(String errorCode) {
        if (StringUtils.isBlank(errorCode)) {
            return "";
        }

        switch (errorCode) {
            case "FailedOperation.DownLoadError":
                return "文件下载失败";
            case "FailedOperation.ImageDecodeFailed":
                return "图片解码失败";
            case "FailedOperation.InvokeChargeError":
                return "调用计费返回失败";
            case "FailedOperation.RequestTimeout":
                return "后端服务超时";
            case "FailedOperation.TooLargeFileError":
                return "文件太大";
            case "FailedOperation.UnKnowError":
                return "内部错误";
            case "FailedOperation.UnOpenError":
                return "服务未开通";
            case "InvalidParameterValue.InvalidParameterValueLimit":
                return "参数值错误";
            case "ResourcesSoldOut.ChargeStatusException":
                return "计费状态异常";
            default:
                return errorCode;

        }


    }


}
